package murphyhan;

import java.util.Calendar;
import java.util.Date;

import org.jdom.Element;

public class GLatLng {
	double lat;
	double lon;
	double ele;
	Date time;

	GLatLng(Element trkpt, int hourOffset) {
		this(trkpt.getAttributeValue("lat"), trkpt.getAttributeValue("lon"));

		Element eleElement = (Element) trkpt.getChildren().get(0);
		ele = Double.parseDouble(eleElement.getText());

		Element timeElement = (Element) trkpt.getChildren().get(1);

		String strTime = timeElement.getText();
		strTime = strTime.replaceAll("Z", "+0000");
		try {
			time = this.getDate(strTime);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(time);
			calendar.add(Calendar.HOUR_OF_DAY, hourOffset);
			this.time = calendar.getTime();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	GLatLng(String lat, String lon) {
		this.lat = Double.parseDouble(lat);
		this.lon = Double.parseDouble(lon);
	}

	GLatLng(double lat, double lon) {
		this.lat = lat;
		this.lon = lon;
	}

	private Date getDate(String time) throws Exception {
		// 2008-05-28T22:26:52+0000
		java.util.Date date = new java.text.SimpleDateFormat(
				"yyyy-MM-dd'T'HH:mm:ssZ").parse(time);
		// System.out.println(date);
		return date;
	}

	public String toString() {
		return "[" + lon + "," + lat + "," + ele + "]";
	}

	public double getAltitudeRate(GLatLng aGLatLng) {
		double eleRate = this.ele / aGLatLng.ele;
		return eleRate;

	}
}
